package cn.eli.vue.controller;

import cn.eli.vue.api.CommonResult;
import cn.eli.vue.entity.User;
import cn.eli.vue.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author :lzs
 * @Date : 2021年7月13日
 * @Description : 用户业务控制器
 */
@Api("用户控制类")
@ResponseBody
@RestController
public class UserBusinessController {
    @Autowired
    UserService userService;

    /*********************************          业务接口    ***********************************************/
    /**
     * 登录验证接口
     * 收取数据包：用户登录账号与密码
     * 传回数据包：所登录用户的信息json字符串 无则为null
     */
    @ApiOperation("登录")
    @RequestMapping(value = "/user/login",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult login(@RequestBody User user){
        String user_loginNumber = user.getUser_loginNumber();
        String user_password = user.getUser_password();

        if(user_loginNumber==null||user_loginNumber.equals("")){
            return CommonResult.validateFailed("账号不可为空");
        }
        if(user_password==null||user_password.equals("")){
            return CommonResult.validateFailed("密码不可为空");
        }

        User user_res = userService.Login(user_loginNumber,user_password);
        if(user_res!=null){
            return CommonResult.success(user_res,"登录成功!");
        }
        else{
            return CommonResult.failed("登录失败 无匹配的用户信息 用户名或密码不正确");
        }
    }

    /**
     * 注册接口
     * 收取数据包：用户信息除id外所有信息属性
     * 传回数据包：
     */
    @ApiOperation("注册")
    @RequestMapping(value = "/user/register" ,method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult register(@RequestBody User user) {
        String user_name = user.getUser_name();
        String user_loginNumber = user.getUser_loginNumber();
        String user_password = user.getUser_password();
        int user_sex = user.getUser_sex();
        String user_avaturl = user.getUser_avaturl();

        if(user_name==null||user_name.equals("")){
            return CommonResult.validateFailed("用户名不可为空");
        }
        if (user_loginNumber==null||user_loginNumber.equals("")) {
            return CommonResult.validateFailed("登录账号不可为空");
        }
        if(user_password==null||user_password.equals("")){
            return CommonResult.validateFailed("密码不可为空");
        }

        /*验证长度*/
        if(user_name.length()>10){
            return CommonResult.validateFailed("用户昵称不可超过10字符长度");
        }
        if(user_loginNumber.length()>11){
            return CommonResult.validateFailed("用户登录账号不可超过11字符");
        }
        if(user_password.length()>16){
            return CommonResult.validateFailed("用户密码不可超过16字符");
        }

        /*验证用户名或用户登录账号重复*/
        User isusernamerepeat = userService.IsUsernameRepeat(user_name);
        User isuserloginnumberrepeat = userService.IsUserLoginNumberRepeat(user_loginNumber);
        if(isuserloginnumberrepeat!=null){
            return CommonResult.validateFailed("用户登录账号已存在");
        }
        if(isusernamerepeat!=null){
            return CommonResult.validateFailed("用户名已存在");
        }

        int user_res = userService.Register(user_name, user_loginNumber, user_password, user_sex, user_avaturl);
        if (user_res!= Integer.MIN_VALUE + 1001&&user_res!=0) {
            return CommonResult.success(user_res, "注册成功");
        } else {
            return CommonResult.validateFailed("注册失败");
        }
    }

    /**
     * 用户信息更改接口
     * 收取数据包：用户除数据外所有信息
     * 传回数据包：
     */
    @ApiOperation("修改信息")
    @RequestMapping(value = "/user/modify",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult Modify(@RequestBody User user){
        int user_id = user.getUser_id();
        String user_name = user.getUser_name();
        String user_loginNumber = user.getUser_loginNumber();
        String user_password = user.getUser_password();
        int user_sex = user.getUser_sex();
        String user_avaturl = user.getUser_avaturl();

        if(user_id == 0){
            return CommonResult.validateFailed("找不到该用户 用户id不存在");
        }
        if (user_name==null||user_name.equals("")){
            return CommonResult.validateFailed("用户名不可为空");
        }
        if (user_loginNumber==null||user_loginNumber.equals("")) {
            return CommonResult.validateFailed("登录账号不可为空");
        }
        if(user_password==null||user_password.equals("")){
            return CommonResult.validateFailed("密码不可为空");
        }
//        if(user_sex==-1){
//            return CommonResult.validateFailed("性别不可为空");
//        }

        /*验证用户名或用户登录账号重复*/
        User isusernamerepeat = userService.IsUsernameRepeat(user_name);
        User isuserloginnumberrepeat = userService.IsUserLoginNumberRepeat(user_loginNumber);
        /*可以为原账号用户名不修改*/
        if(isuserloginnumberrepeat!=null&&isuserloginnumberrepeat.getUser_id()!=user_id){
            return CommonResult.validateFailed("用户登录账号已存在");
        }
        if(isusernamerepeat!=null&&isusernamerepeat.getUser_id()!=user_id){
            return CommonResult.validateFailed("用户名已存在");
        }

        int user_res = userService.Modify( user_id, user_name,  user_loginNumber,  user_password,  user_sex,  user_avaturl);
        /*结果不超界且影响行数不为0*/
        if(user_res!=Integer.MIN_VALUE + 1001&&user_res!=0){
            return CommonResult.success(user_res,"内容修改成功");
        }
        else{
            return CommonResult.validateFailed("修改内容失败");
        }
    }

    /*********************************             工具接口         ****************************************/
    /**
     * 用户名称重复验证接口
     * 收取数据包：用户名
     * 传回数据包：重复或空
     */
    @ApiOperation("检查用户名重复")
    @RequestMapping(value = "/user/isusernamerepeat",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult IsUsernameRepeat(@RequestBody User user){
        String user_name = user.getUser_name();

        if(user_name==null||user_name.equals("")){
            return CommonResult.validateFailed("用户名不可为空");
        }

        User user_res = userService.IsUsernameRepeat(user_name);
        if(user_res!=null){
            return CommonResult.validateFailed("用户名已存在");
        }
        else{
            return CommonResult.success(null,"无重复用户名");
        }
    }

    /**
     * 用户登录账号重复验证接口
     * 收取数据包：用户登录账号
     * 传回数据包：重复信息或空
     */
    @ApiOperation("检查账号重复")
    @RequestMapping(value = "/user/isuserloginnumberrepeat",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult IsUserLoginNumberRepeat(@RequestBody User user){
        String user_loginNumber = user.getUser_loginNumber();

        if (user_loginNumber==null||user_loginNumber.equals("")) {
            return CommonResult.validateFailed("登录账号不可为空");
        }

        User user_res = userService.IsUserLoginNumberRepeat(user_loginNumber);
        if(user_res!=null){
            return CommonResult.validateFailed("用户登录账号已存在");
        }
        else{
            return CommonResult.success(null,"无重复用户登录账号");
        }
    }

    /**
     * 用户名检索用户实体
     * 收取数据包 用户名
     * 返回数据包 用户实体
     */
    @ApiOperation("从用户名返回用户实体")
    @RequestMapping(value = "/user/usernametouser",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult UsernameToUser(@RequestBody User user){
        String user_name = user.getUser_name();

        if(user_name==null||user_name.equals("")){
            return CommonResult.validateFailed("用户名不可为空");
        }

        User user_res = userService.usernameToUser(user_name);
        if(user_res!=null){
            return CommonResult.success(user_res,"查询的用户信息已返回");
        }
        else{
            return CommonResult.validateFailed("该用户不存在");
        }
    }

}
